package com.ruoyi.resume.utils;

import com.ruoyi.resume.domain.model.Category;
import com.ruoyi.resume.domain.model.Tag;
import com.ruoyi.resume.mapper.CategoryMapper;
import com.ruoyi.resume.mapper.TagMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class LabelingUtils {

    @Autowired
    private CategoryMapper categoryMapper; // 查询分类表
    @Autowired
    private TagMapper tagMapper; // 查询标签表

    public List<Tag> getTagsByCategoryId(Integer categoryId) {
        if (categoryId == null) {
            throw new IllegalArgumentException("Category ID 不能为空");
        }

        // 查询当前 Category 信息
        Category category = categoryMapper.selectCategoryById(categoryId);
        if (category == null) {
            throw new IllegalArgumentException("未找到 ID 为 " + categoryId + " 的 Category");
        }

        // 如果是最后一级，直接查找对应的标签
        if (Boolean.TRUE.equals(category.getIsLastLevel())) {
            return tagMapper.selectTagsByCategoryId(categoryId);
        }

        // 如果不是最后一级，递归查找所有子分类的标签
        List<Tag> allTags = new ArrayList<>();
        List<Category> children = categoryMapper.selectCategoriesByParentId(categoryId);

        for (Category child : children) {
            // 递归获取子分类的标签
            allTags.addAll(getTagsByCategoryId(child.getId()));
        }

        return allTags;
    }
}
